package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Name;
import com.example.demo.entity.R;
import com.example.demo.mapper.LogMapper;
import com.example.demo.service.ILogService;
import com.example.demo.utils.JwtUtil;
import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Api(tags = "manageSystem")
@RestController
@RequestMapping("/manageSystem")
public class Vue2ManageSystemController {

    @Autowired
    ILogService logService;
    @Autowired
    LogMapper logMapper;

    private static long tokenExpiration = 24*60*60*1000;
    //自定义秘钥
    private static String tokenSignKey = "123456";
    public static String createToken(String userName, String password) {
        String token = Jwts.builder()
                //设置分组
                .setSubject("YYGH-USER")
                //设置字符串过期时间
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
                //私有部分
                .claim("userName", userName)
                .claim("password", password)
                //设置秘钥
                .signWith(SignatureAlgorithm.HS512, tokenSignKey)
                .compressWith(CompressionCodecs.GZIP)
                .compact();
        return token;
    }
    @ApiOperation("获取名字列表")
    @RequestMapping("/login")
    public R login(@RequestBody JSONObject request) {
        Set set = request.entrySet();
        String username = (String) request.get("username");
        String password = (String) request.get("password");
        System.out.println("getName")   ;
        List<Map<String,Object>> result = logService.loginUser(username,password);

        if(result!=null){
            String token = JwtUtil.createToken(username,password);
            return R.success("true",token);
        }
        return R.error("账户或密码错误");
    }
    @ApiOperation("获取名字列表")
    @RequestMapping("/getName")
    public List<Name> getName() {
        System.out.println("getName");
        List<Name> result = logService.getName();
//        for (String s:
//                result) {
//            System.out.println(s);
//        }
        System.out.println(logService.getName().get(0));
        return logService.getName();
    }

    @PostMapping("/pageTest")
    public IPage<Name> pageTest(HttpServletRequest request){
        Integer currentPage = Integer.valueOf(request.getParameter("currentPage"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        String a= "1";
        Page<Name> page = new Page<Name>(currentPage, pageSize);
        IPage<Name> resultList = logMapper.getPageName(page);
        return resultList;
    }
    @ApiOperation("获取名字列表")
    @RequestMapping("/loginVue3")
//    public Boolean loginVue3(@RequestParam("username")String username,@RequestParam("password")String password) {
    public Boolean loginVue3(@RequestBody JSONObject request) {
        Set set = request.entrySet();
        String username = request.getString("username");
        String password = request.getString("password");
        System.out.println("getName")   ;
        List<Map<String,Object>> result = logService.loginUser(username,password);
        if(result.size()>0){
            return true;
        }
        return false;
    }
}
